{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 88. 合并两个有序数组\n",
    "# 双指针，从后往前遍历\n",
    "def merge(nums1, nums2):\n",
    "    n1,n2 = len(nums1),len(nums2)\n",
    "\n",
    "    p1,p2,p=n1-1,n2-1,n1+n2-1 # 都指向数组末尾\n",
    "    nums1 += [0 for i in range(n2)] # 扩展数组num1长度\n",
    "\n",
    "    while p1>=0 and p2>=0:\n",
    "        # nums1[p-=1]=nums2[p2-=1] if nums1[p1]<nums2[p2] else nums1[p1-=1]\n",
    "        if nums1[p1]<nums2[p2]:\n",
    "            nums1[p]=nums2[p2]\n",
    "            p-=1\n",
    "            p2-=1\n",
    "        else:\n",
    "            nums1[p]=nums1[p1]\n",
    "            p-=1\n",
    "            p1-=1\n",
    "    return nums1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 2, 3, 5, 6]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nums1 = [1,2,3]\n",
    "nums2 = [2, 5,6]\n",
    "\n",
    "merge(nums1, nums2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify nums1 in-place instead.\n",
    "        \"\"\"\n",
    "        p1, p2, p = m - 1, n - 1, m + n - 1\n",
    "        # while p1 >= 0 and p2 >= 0:\n",
    "        while p2 >= 0:  # 存在num1为空的情况,只需把num2合并到num1就行.\n",
    "            if nums1[p1] < nums2[p2]:\n",
    "                nums1[p] = nums2[p2]\n",
    "                p2 -= 1\n",
    "                print(nums1)\n",
    "            else:\n",
    "                nums1[p] = nums1[p1]\n",
    "                print(nums1)\n",
    "                p1 -= 1\n",
    "            p -= 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1]\n"
     ]
    }
   ],
   "source": [
    "nums1 = [0]\n",
    "nums2 = [1]\n",
    "Solution().merge(nums1, 0, nums2, 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nums1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
